package com.puny.mvc.controllers
{
	import com.puny.mvc.MvcEvent;
	import com.puny.testframework.TestCaseWithStage;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	dynamic public class ControllerTest extends TestCaseWithStage
	{
		private var ctrl:SampleController;
		private var ctrl2:ExternalFileController;
		[Embed(source="assets/scholastic_choose_chara.swf")]
		private var swf:Class;
		public function ControllerTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testController():void {
			ctrl = new SampleController();
			ctrl.addEventListener(MvcEvent.READY, addAsync(readyHandler, 4000));
			ctrl.addEventListener(MvcEvent.COMPLETED, addAsync(completeHandler, 4001));
			var con:Controller = new Controller();
			con.addChild(ctrl)
			mainStage.addChild(con);
		}
		
		private function readyHandler(event:Event):void {
			assertEquals(2, event.currentTarget.numChildren);
		}
		
		private function completeHandler(event:Event):void {
			assertEquals(3, ctrl.checker);
			var timer:Timer = new Timer(3000,1);
			timer.addEventListener(TimerEvent.TIMER, addAsync(checkHandler,40002));
			timer.start();
		}
		private function checkHandler(event:Event):void {
			assertEquals(5, ctrl.checker);
			if (ctrl.numChildren > 0) {
				trace(ctrl.getChildAt(0));
			}
			assertEquals(0, ctrl.numChildren);
		}
		
		public function testExternalConfiguration():void {
			ctrl2 = new ExternalFileController();
			ctrl2.addEventListener(MvcEvent.READY, addAsync(readyHandler2,4003));
			mainStage.addChild(ctrl2);
		}
		
		private function readyHandler2(event:Event):void {
			trace("external file is loaded");
		}
		
	}
}